<style>
td {padding:5px 10px;}
.p-list td{line-height:40px !important;}
</style>
<?php $all_virtual = true; foreach($Carts as $cart) { if(!$cart['Cart']['is_virtual']){$all_virtual=false;break;}  } ?>

<script>
var setAmount={
		min:1,
		max:9999,
		reg:function(x){
			return new RegExp("^[1-9]\\d*$").test(x);
		},
		amount:function(obj,mode){
			var x=$(obj).val();
			if (this.reg(x)){
				if (mode){
					x++;
				}else{
					x--;
				}
			}else{
				alert("请输入正确的数量！");
				$(obj).val(1);
				$(obj).focus();
			}			
			return x;
		},
		reduce:function(obj){
			var x=this.amount(obj,false);
			if (x>=this.min){
				$(obj).val(x);
			}else{
				alert("商品数量最少为"+this.min);
				$(obj).val(this.min);
				$(obj).focus();
			}
			editCartNum($(obj).data('id'),$(obj).val());
		},
		add:function(obj){
			var x=this.amount(obj,true);
			if (x<=this.max){
				$(obj).val(x);
			}else{
				alert("商品数量最多为"+this.max);
				$(obj).val(this.max);
				$(obj).focus();
			}
			editCartNum($(obj).data('id'),$(obj).val());
		},
		modify:function(obj){
			var x=$(obj).val();
			if (x<this.min||x>this.max||!this.reg(x)){
				alert("请输入正确的数量！");
				$(obj).val(this.min);
				$(obj).focus();
			}
		}
}
var last_hash = randomString(10);
/* 设置一个hash值校验，防止联系多次点击时，返回的先后顺序不对覆盖成过期数据了 */
var refreshCartContainer = function(request,moreags){
	if(request.hash == last_hash) {
		var html = '<div>' + request.carts_html + '</div>';
		var newhtml = $(html).find('#cart-list-container').html();
		$('#cart-list-container').html(newhtml);
	}
	else{
		// hash 校验数据不对的直接抛弃
	}
}
function editCartNum(id,num){
	last_hash = randomString(10);
	var url=BASEURL+'/carts/editCartNum/'+id+'/'+num+'?type=info&ck_ids={{$_REQUEST['ck_ids']}}&hash='+last_hash;	
	// var postdata = {'data[Cart][num]':num,'data[Cart][product_id]':id};
	if(!sso.check_userlogin({"callback":editCartNum,"callback_args":arguments}))
		return false;
	ajaxAction(url,null,null,refreshCartContainer);
	return false;
}

$(function(){
	
	$(document).on('keyup','.cart-num-input',function(){
		// @todo 检查为字母符号等键时跳过。只支持数字，剪切，粘贴,回退删除		
		if($(this).val() > 0 ) {
			editCartNum($(this).data('id'),$(this).val());
		}		
	});
})

function deleteCartItem(id){
	last_hash = randomString(10);
	var url=BASEURL+'/carts/delete/'+id+'?hash='+last_hash;	
	// var postdata = {'data[Cart][num]':num,'data[Cart][product_id]':id};
	if(!sso.check_userlogin({"callback":deleteCartItem,"callback_args":arguments}))
		return false;
	
	ajaxAction(url,null,null,refreshCartContainer);
	return false;
}

function submitOrder(form){
	{{if !$all_virtual}}
	if($('input[name="data[Order][consignee_id]"]:checked').size() == 0){
		showErrorMessage("{{__('Please choose or add a new address at first')}}");
		return false;
	}
	{{/if}}
	return true;
}
</script>

<ol class="breadcrumb">
		  <li><a href="/users/index"><i class="fa fa-home"></i>&nbsp;&nbsp;{{__('Personal Center')}}</a></li>
		  <li><a href="/orders/mine">{{__('My Orders')}}</a></li>
		  <li class="active">订单信息</li>
		</ol>           
       <hr>
       

{{$this->Form->create('Order',array(
	'class'=>'form-horizontal',
	'role'=>"form",
	'url'=> $this->Html->url('/orders/balance'),
	'onsubmit' => 'return submitOrder(this);',
))}}
	<div class="orderinfo">
		<div class="mt10">
			{{if $orderinfo['Order']['status'] == 1}}
				<div class="alert alert-success">订单已支付</div>
			{{elseif $orderinfo['Order']['status'] == -1}}
				<div class="alert alert-success">订单已取消</div>
			{{/if}}
		</div>
		{{if !$all_virtual}}{{/if}}
		
		<div id="part_payTypeAndShipType"><!-- template orders/info_shiptype--></div>
		
		<!-- 
		{{if $order_id}}
			<div id="part_invoice">--template orders/order_invoice</div>
		{{else}}
			<div id="part_invoice">--template orders/edit_invoice</div>
		{{/if}}
		 -->
		


		<div id="cart-list-container">
		<div class="ui-portlet-header">
			      <span class="title">商品清单</span>       
			      {{if !$order_id}}<small><a class="pull-right" style="color:#0070D7;margin:10px 5px" href="{{$this->Html->url('/carts/listcart')}}" id="backToCartBtn">返回修改购物数量</a></small>{{/if}}     
		</div>
		<p><br/></p>	
		{{if $Carts[0]['Cart']['product_model']=='Role'}}
		<p>优惠提示：一次性购买12个月135编辑器VIP会员（即一年），优惠两个月，只收取10个月的费用。</p>
		<p><span style="color:red;">好消息，支付满50元可申请<a target="_blank" href="/order_invoices/mine">开具发票</a></span>。</p>
		{{/if}}

				

			<?php $origin_total = 0; ?>
		<div class="p-list">
			<table  class="table table-bordered">
			<tr><th width="50%">商品名称</th><th>购买价格</th><th>商品数量</th><th class="text-right">合计</th></tr>
			<tbody>
			{{loop $Carts $cart}}
			<tr><td>
				{{if $cart['Cart']['product_id']}}
				<a href="{{Router::url('/'.Inflector::tableize($cart['Cart']['product_model']).'/view/'.$cart['Cart']['product_id'])}}" target="_blank">{{$cart['Cart']['name']}}</a>
				{{else}}
				{{$cart['Cart']['name']}}
				{{/if}}

			</td>
			<td>{{format_price($cart['Cart']['price'])}}</td>
			<td>
{{if !$order_id}}
<div class="input-group" style="float:left;width:135px;">
<span class="reduce input-group-addon" onclick="setAmount.reduce('#pamount-{{$cart['Cart']['id']}}')">-</span>
<input type="number" size="4" id="pamount-{{$cart['Cart']['id']}}" data-id="{{$cart['Cart']['id']}}" data-price="{{$cart['Cart']['price']}}" class="form-control cart-num-input" name="data[Cart][{{$cart['Cart']['id']}}][num]" value="{{$cart['Cart']['num']}}" />
<span class="add input-group-addon" onclick="setAmount.add('#pamount-{{$cart['Cart']['id']}}')">+</span>
</div>
{{else}}
	{{format_price($cart['Cart']['num'])}}
{{/if}}
			
			</td>
			<td class="text-right">
				<span class=" product-price"><small>{{format_price($cart['Cart']['price']*$cart['Cart']['num'])}}</small></span>
				<?php $origin_total += $cart['Cart']['price']*$cart['Cart']['num']; ?>
			</td>		
			</tr>
			{{/loop}}
			</tbody>
			</table>
		</div>		
	{{if $Carts[0]['Cart']['is_virtual']}}
		<p style="color:red;">*温馨提示：虚拟会员一经售出不予退款，请根据需要选择。</p>
	{{/if}}
{{if $Carts[0]['Cart']['product_model'] == 'Role'}}
	<p>一次性购买年费会员，可获赠人人秀5000个精品H5模板，以及幕布、创客贴、番茄土豆一个月会员体验。</p>
{{/if}}
		
		
		<!-- template orders/info_wave  -->
		<!-- 
		<div id="part_remark">
			<div>
			    <h3>订单备注</h3>
			    <div class="middle">
			        <input type="text" name="data[Order][remark]" maxlength="200" value="" class="form-control" style="width:300px;" id="order_remark">   &nbsp;&nbsp;&nbsp;
			    </div>			    
			</div>
		</div> -->
		
		<div class="text-right clearfix">
			{{if $ship_fee > 0}}
			<p>快递费用：<span id="total-price">{{format_price($ship_fee)}}</span></p>
			{{/if}}
			
			{{if $total_price!= $origin_total}}
			<p>总价：<span style="width: 60px;display: inline-block;text-decoration: line-through;">{{format_price($origin_total)}}</span></p>
			<p>优惠：<span style="width: 60px;display: inline-block;">{{format_price($origin_total-$total_price)}}</span></p>
			{{/if}}
			
			<p>合计：<span id="total-price" class=" product-price">{{format_price($order_total_price)}}</span></p>
		
		{{if !$order_id}}	
		<p><button class="btn btn-warning" style="padding:8px 35px;" type="submit">支付</button></p>
		{{elseif !empty($orderinfo) && $orderinfo['Order']['status'] == 0 }}
				<a href="{{$this->Html->url('/orders/pay/'.$order_id)}}" class="btn btn-warning">立即支付</a>
		{{/if}}	
		</div>
		</div>
		<div style="clear:both;"></div>
		{{if $order_id && $orderinfo['Order']['status'] > 0}}	
		<div class="clearfix">
			<div class="ui-portlet-header">
			      <span class="title">支付记录</span>            
			</div>
			<table class="table">
			<thead>
			<tr><td>支付方式</td><td>交易号</td><td>支付费用</td><td>支付时间</td></tr>
			</thead>
			<portlet portlet="body" cache="no" title="支付记录" list_tpl="inner" model="PayLog" info="conditions[order_id]=$order_id">
			<tr><td>{{$item['pay_method']}}</td><td>{{$item['transaction_id']}}</td><td>{{$item['total_fee']}}</td><td>{{$item['created']}}</td></tr>
			</portlet>
			</table>
		</div>
		{{/if}}	
	</div>
</form>